//
// Created by yuping.li on 10/17/24.
//

#ifndef LOG_HPP_
#define LOG_HPP_

#define INFO    1
#define WARNING 2
#define ERROR   3
#define FATAL   4

#define SWEN_INFO    INFO
#define SWEN_WARNING WARNING
#define SWEN_ERROR   ERROR
#define SWEN_FATAL   FATAL

#ifndef LOG_LEVEL
#define LOG_LEVEL INFO
#endif

#ifndef LOG
#include <cstdio>
#define LOG printf
#endif

#define CALMCAR_LOG_IS_ON(severity) (LOG_LEVEL <= severity)

#define CALMCAR_LOG_IF(condition, format_string, ...)          \
    if (condition) {                                                    \
        LOG(format_string" [%s:%d]\r\n", ##__VA_ARGS__, __FUNCTION__, __LINE__);             \
    }

#define CALMCAR_LOG_IF_EVERY_N(condition, n, format_string, ...)   \
    {                                                                       \
    static uint32_t __swen_log_count = 0;                                   \
    if ((!(__swen_log_count % n)) || (__swen_log_count == 0)) {             \
        CALMCAR_LOG_IF(condition, format_string, ##__VA_ARGS__);            \
    }                                                                       \
    ++__swen_log_count;                                                     \
    }


#define CCLOG(severity, format_string, ...)                                 \
    CALMCAR_LOG_IF(CALMCAR_LOG_IS_ON(severity), format_string, ##__VA_ARGS__)

#define CCLOG_EVERY_N(severity, n, format_string, ...)                      \
    CALMCAR_LOG_IF_EVERY_N(CALMCAR_LOG_IS_ON(severity), n,                \
                          format_string, ##__VA_ARGS__)

#define CCLOG_IF(severity, condition, format_string, ...) \
    CALMCAR_LOG_IF(condition, CALMCAR_LOGGER(severity), format_string, ##__VA_ARGS__)


#endif //LOG_HPP_
